package com.reading_recognition_system.controller;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baidu.aip.ocr.AipOcr;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/api" )
public class ApiController {
    private final AipOcr aipOcr;

    @Autowired
    public ApiController(AipOcr aipOcr) {
        this.aipOcr = aipOcr;
    }
    @RequestMapping("/ocr")
    @ResponseBody
    public ResponseEntity<String> ocr(MultipartFile image) throws IOException {

        //获取图片
        byte[] imageData = image.getBytes();
        // 调用通用文字识别接口
        JSONObject res = aipOcr.basicGeneral(imageData, null);
        //返回结果
        return ResponseEntity.ok(res.toString());
    }
}
